package rs.etf.es.algorithm;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class LambdaMessages implements Serializable {

    private HashMap<String, HashMap<String, Double>> lambdaMessagesMap;

    public LambdaMessages() {
	lambdaMessagesMap = new HashMap<String, HashMap<String, Double>>();
    }

    /**
     * @param childName
     *            is name of sender of lambda message
     * @param variableValueName
     * @param messageValue
     * 
     *            Adds lambda message
     */
    public void addLambdaMessage(String childName, String variableValueName, Double messageValue) {
	HashMap<String, Double> variablesMap = lambdaMessagesMap.get(childName);
	if (variablesMap == null) {
	    variablesMap = new HashMap<String, Double>();
	    lambdaMessagesMap.put(childName, variablesMap);
	}
	variablesMap.put(variableValueName, messageValue);
    }

    public Double getLambdaMessage(String childName, String variableValueName) {
	HashMap<String, Double> variablesMap = lambdaMessagesMap.get(childName);
	if (variablesMap == null) {
	    return null;
	} else {
	    return variablesMap.get(variableValueName);
	}
    }

    @SuppressWarnings("rawtypes")
    @Override
    public String toString() {
	StringBuilder returnString = new StringBuilder();

	ArrayList<String> allKeys = new ArrayList<String>(lambdaMessagesMap.keySet());
	for (String key : allKeys) {
	    returnString.append("Lambda message sender: {" + key + "}\n");
	    HashMap<String, Double> mapForThisKey = lambdaMessagesMap.get(key);
	    Set entrySet = mapForThisKey.entrySet();

	    Iterator iter = entrySet.iterator();

	    while (iter.hasNext()) {
		Map.Entry entry = (Map.Entry) iter.next();
		returnString.append(entry.getKey() + "->" + entry.getValue() + "\n");
	    }
	}

	return returnString.toString();
    }
}
